package com.cy.model;

import com.cy.BasicModel;
import com.cy.ext.plugin.tablebind.TableBind;
import com.jfinal.plugin.activerecord.Page;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* Created with IntelliJ IDEA.
* User: Administrator
* Date: 13-9-9
* Time: 下午12:56
* To change this template use File | Settings | File Templates.
*/
@SuppressWarnings("serial")
@TableBind(tableName="license_token_log",pkName = "log_id")
public class LicenseTokenLog extends BasicModel<LicenseTokenLog> {
    public static final LicenseTokenLog dao = new LicenseTokenLog();
    public static final String SELECT_ALL = "`log_id`,`user_id`,`mobile`,`operation_time`,`license_token_id`,`license_token_code`,`remark`,`type` ";


    @Override
    public Page<LicenseTokenLog> paginate(Integer pageNumber, Integer pageSize, String column, LicenseTokenLog log) {
        List<Object> paras = new ArrayList<>();
        StringBuffer sqlExceptSelectBuffer = new StringBuffer("FROM " + getTableName());
//        sqlExceptSelectBuffer.append(other);
        sqlExceptSelectBuffer.append(" where 1=1 ");
        StringBuffer selectBuffer = new StringBuffer("SELECT ").append(column);
        if (pageNumber == null || pageNumber == 0) {
            pageNumber = 1;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = 20;
        }

        if (log != null) {
            String[] attrNames = log.getAttrNames();
            for (String attrName : attrNames) {
                Object attrValue = log.get(attrName);
                if (attrValue != null) {

                    if("start".equals(attrName)){
                        sqlExceptSelectBuffer.append(" AND `operation_time` > DATE_FORMAT(?,'%Y-%m-%d %T')");
                    }else if("end".equals(attrName)){
                        sqlExceptSelectBuffer.append(" AND `operation_time` < DATE_FORMAT(?,'%Y-%m-%d %T')");
                    }else if("license_token_code".equals(attrName)){
                        sqlExceptSelectBuffer.append(" AND `license_token_code` like ?");
                        attrValue = "%"+attrValue+"%";
                    }else{
                       /* int index = attrName.indexOf(".");
                        if (index > 0){
                            sqlExceptSelectBuffer.append(" AND `"+attrName.substring(0,index)+"`.`" + attrName.substring(index+1) + "`=?");
                        }else {*/
                        sqlExceptSelectBuffer.append(" AND `" + attrName + "`=?");
//                        }
                    }
                    paras.add(attrValue);
                }
            }
        }

        if (paras.size() > 0) {
            return paginate(pageNumber, pageSize, selectBuffer.toString(), sqlExceptSelectBuffer.toString(), paras.toArray());
        }
        return paginate(pageNumber, pageSize, selectBuffer.toString(), sqlExceptSelectBuffer.toString());
    }

    public LicenseTokenLog setLogId (Integer logId){
        this.set("log_id",logId);
        return this;
    }

    public Integer getLogId (){
        return this. get("log_id");
    }

        public LicenseTokenLog setUserId (Long userId){
        this.set("user_id",userId);
        return this;
    }

    public Long getUserId (){
        return this. getLong("user_id");
    }

        public LicenseTokenLog setMobile (String mobile){
        this.set("mobile",mobile);
        return this;
    }

    public String getMobile (){
        return this.getStr("mobile");
    }

        public LicenseTokenLog setOperationTime (Date operationTime){
        this.set("operation_time",operationTime);
        return this;
    }

    public Date getOperationTime (){
        return this.getDate("operation_time");
    }

        public LicenseTokenLog setLicenseTokenId (Long licenseTokenId){
        this.set("license_token_id",licenseTokenId);
        return this;
    }

    public Long getLicenseTokenId (){
        return this. getLong("license_token_id");
    }

        public LicenseTokenLog setLicenseTokenCode (String licenseTokenCode){
        this.set("license_token_code",licenseTokenCode);
        return this;
    }

    public String getLicenseTokenCode (){
        return this.getStr("license_token_code");
    }

        public LicenseTokenLog setRemark (String remark){
        this.set("remark",remark);
        return this;
    }

    public String getRemark (){
        return this.getStr("remark");
    }

    public LicenseTokenLog setType (Integer type){
        this.set("type",type);
        return this;
    }

    public Integer getType (){
        return this. get("type");
    }
}
